//
//  MeBindEmailViewController.m
//  DBAChain
//
//  Created by Mxionlly on 2018/9/15.
//  Copyright © 2018年 Evan. All rights reserved.
//

#import "MeBindEmailViewController.h"
#import "taskUserManagerService.h"

@interface MeBindEmailViewController ()
@property (weak, nonatomic) IBOutlet UIButton *codeButton;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (weak, nonatomic) IBOutlet UITextField *emailField;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutToView;
@property (weak, nonatomic) IBOutlet UIButton *bindButton;
@property (nonatomic, strong) NSTimer           *timer;
@property (nonatomic, assign) NSInteger         number;
@end

@implementation MeBindEmailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"邮箱绑定";
    self.number = 60;
    self.layoutToView.constant =100;
    self.bindButton.backgroundColor = COLOR_main;
    self.bindButton.layer.masksToBounds = YES;
    self.bindButton.layer.borderWidth = 1.0f;
    self.bindButton.layer.borderColor = [UIColor clearColor].CGColor;
    self.bindButton.layer.cornerRadius = 20.0f;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setBlackStatus:YES];
}

- (IBAction)getCodeAction:(id)sender {
    if (![Check validateEmail:self.emailField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的邮箱地址"];
        return;
    }else{
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setField:self.emailField.text value:@"emailaddress"];
        [DKProgressHUD showLoading];
        [taskUserManagerService getSendEmailVerifyCode:dict success:^(BaseModel *response, BOOL success) {
            [DKProgressHUD dismiss];
            if (success) {
                self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updataAction) userInfo:nil repeats:YES];
                self.codeButton.userInteractionEnabled = NO;
                [DKProgressHUD showSuccessWithStatus:@"发送成功"];
            }else{
                [DKProgressHUD showErrorWithStatus:response.message];
            }
        } failture:^(NSError *error, NSString *strDescription) {
            [DKProgressHUD dismiss];
            [DKProgressHUD showErrorWithStatus:strDescription];
        }];
    }
}

- (void)updataAction {
    self.number--;
    [self.codeButton setTitle:[NSString stringWithFormat:@"%lds",(long)self.number] forState:UIControlStateNormal];
    if (self.number <= 0) {
        [self closeTimer];
    }
}

- (void)closeTimer {
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
        self.number = 60;
        self.codeButton.userInteractionEnabled = YES;
        [self.codeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    }
}

- (IBAction)bindEmailAction:(id)sender {
    if (![Check validateEmail:self.emailField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的邮箱地址"];
        return;
    }
    if (![Check checkLenght:self.codeField.text]) {
        [DKProgressHUD showErrorWithStatus:@"您输入邮件验证码"];
        return;
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setField:self.emailField.text value:@"email"];
    [dict setValue:self.codeField.text forKey:@"code"];
    [DKProgressHUD showLoading];
    [taskUserManagerService getBindEmail:dict success:^(BaseModel *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            [[UserCenter shareCenter] getProfiles:^(dataGetUserInfo *profile) {
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }else{
            [DKProgressHUD showErrorWithStatus:response.message];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
